package com.ruoyi.WELD.controller;

import com.ruoyi.WELD.domain.Weld;
import com.ruoyi.WELD.service.IWeldService;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.utils.poi.ExcelUtil;
import org.springframework.beans.factory.annotation.Autowired;
import javax.servlet.http.HttpServletResponse;
import java.util.List;
import java.util.Objects;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

@RestController
@RequestMapping("/Weld/weld")
public class WeldController extends BaseController
{
    @Autowired
    private IWeldService weldService;

    /*查询*/
    @GetMapping("/list")
    public TableDataInfo list(Weld weld) {
        startPage();
        List<Weld> list = weldService.selectWeldList(weld);
        return getDataTable(list);
    }

    /*获取*/
    @GetMapping(value = "/{ID}")
    public AjaxResult getInfo(@PathVariable("ID") Long ID)
    {
        return success(weldService.selectWeldById(ID));
    }

    /*新增*/
    @PostMapping
    public AjaxResult add(@RequestBody Weld weld)
    {
        return toAjax(weldService.insertWeld(weld));
    }

    /*修改*/
    @PutMapping
    public AjaxResult edit(@RequestBody Weld weld)
    {
        return toAjax(weldService.updateWeld(weld));
    }

    /*删除*/
    @DeleteMapping("/{IDs}")
    public AjaxResult remove(@PathVariable Long[] IDs)
    {
        return toAjax(weldService.deleteWeldByIds(IDs));
    }

    /*清空数据*/
    @DeleteMapping("/clear")
    public AjaxResult clearAllWelding()
    {
        try{
            weldService.ClearWeld();
            return toAjax(1);
        }catch (Exception e){
            return toAjax(0);
        }
    }

    /*导出*/
    @PostMapping("/export")
    public void export(Long[] ids,HttpServletResponse response)
    {
        List<Weld> list = weldService.selectWeldByIds(ids);
        ExcelUtil<Weld> util = new ExcelUtil<>(Weld.class);
        util.exportExcel(response, list, "数据");
    }

    /*导出全部，默认全选*/
    @PostMapping("/exportAll")
    public void exportAll(Weld weld, HttpServletResponse response)
    {
        List<Weld> list = weldService.selectWeldList(weld);
        ExcelUtil<Weld> util = new ExcelUtil<>(Weld.class);
        util.exportExcel(response, list, "数据");
    }

    /*导入模版*/
    @PostMapping("/importTemplate")
    public void importTemplate(HttpServletResponse response)
    {
        ExcelUtil<Weld> util = new ExcelUtil<>(Weld.class);
        util.importTemplateExcel(response, "数据");
    }

    /*导入*/
    @PostMapping("/importData")
    public AjaxResult importData(MultipartFile file, boolean updateSupport) throws Exception
    {
        ExcelUtil<Weld> util = new ExcelUtil<>(Weld.class);
        List<Weld> weldList = util.importExcel(file.getInputStream());
        // 1. 去掉 ExcelUtil 塞进来的 null
        weldList.removeIf(Objects::isNull);
        String message = weldService.importWeld(weldList, updateSupport);
        return success(message);
    }
}